﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    [CanEditMultipleObjects, CustomEditor(typeof(Animation))]
    internal class AnimationEditor : Editor
    {
        private BoxEditor m_BoxEditor = new BoxEditor(false, s_BoxHash);
        private SerializedProperty m_UserAABB;
        private static int s_BoxHash = "AnimationBoundsEditorHash".GetHashCode();

        internal override void OnAssetStoreInspectorGUI()
        {
            this.OnInspectorGUI();
        }

        public void OnDisable()
        {
            this.m_BoxEditor.OnDisable();
        }

        public void OnEnable()
        {
            this.m_UserAABB = base.serializedObject.FindProperty("m_UserAABB");
            this.m_BoxEditor.OnEnable();
        }

        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            EditorGUIUtility.LookLikeInspector();
            string[] propertyToExclude = new string[] { "m_UserAABB" };
            Editor.DrawPropertiesExcluding(base.serializedObject, propertyToExclude);
            EditorGUIUtility.LookLikeControls();
            Animation target = (Animation) base.target;
            if ((target != null) && (target.cullingType == AnimationCullingType.BasedOnUserBounds))
            {
                EditorGUILayout.PropertyField(this.m_UserAABB, new GUIContent("Bounds"), new GUILayoutOption[0]);
                if (GUI.changed)
                {
                    EditorUtility.SetDirty(base.target);
                }
            }
            base.serializedObject.ApplyModifiedProperties();
        }

        public void OnSceneGUI()
        {
            Animation target = (Animation) base.target;
            if ((target != null) && ((target.cullingType == AnimationCullingType.BasedOnClipBounds) || (target.cullingType == AnimationCullingType.BasedOnUserBounds)))
            {
                this.m_BoxEditor.SetAlwaysDisplayHandles(target.cullingType == AnimationCullingType.BasedOnUserBounds);
                Undo.SetSnapshotTarget(target, "Modified Animation bounds");
                Bounds localBounds = target.localBounds;
                Vector3 center = localBounds.center;
                Vector3 size = localBounds.size;
                if (this.m_BoxEditor.OnSceneGUI(target.transform, Handles.s_BoundingBoxHandleColor, ref center, ref size))
                {
                    target.localBounds = new Bounds(center, size);
                }
            }
        }
    }
}

